package com.weixing.mall.provider.controller.admin;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.model.dto.CategoryChgEnableDto;
import com.weixing.mall.provider.model.dto.CategoryDto;
import com.weixing.mall.provider.model.query.CategoryQuery;
import com.weixing.mall.provider.model.vo.CategoryTreeVo;
import com.weixing.mall.provider.model.vo.CategoryVo;
import com.weixing.mall.provider.service.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName CategoryController
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/11/20
 * @Version 1.0
 **/
@RestController
@RequestMapping("category")
@Api(value = "商品类目APi", tags = "商品类目")
public class CategoryController extends BaseController {

    @Autowired
    private ICategoryService categoryService;


    @ApiOperation(value = "根据条件加载商品类目")
    @GetMapping("getList")
    public Result list(CategoryQuery categoryQuery) {
        return ResultUtil.success(categoryService.selectList(categoryQuery));
    }

    @ApiOperation(value = "根据条件加载可用商品类目")
    @GetMapping("getableList")
    public Result getableList(CategoryQuery categoryQuery) {
        categoryQuery.setIsEnable(WhetherEnum.YES.getValue());
        return ResultUtil.success(categoryService.selectList(categoryQuery));
    }


    @ApiOperation(value = "保存商品类目")
    @PostMapping("save")
    @NoRepeatSubmit
    public Result save(@Validated CategoryDto categoryDto) throws Exception{
     /*   CategoryQuery categoryQuery=new CategoryQuery();
        categoryQuery.setCatLevel(categoryDto.getCatLevel());
        categoryQuery.setName(categoryDto.getCatName());
        List<CategoryVo> categoryList=categoryService.selectList(categoryQuery);
        if(categoryList.size()>0){
            if(!categoryList.get(0).getCatName().equals(categoryDto.getCatName())){
                return ResultUtil.error("该分类层级下已经存在相同的分类名称");
            }

        }*/
        categoryService.save(categoryDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "启用/禁用")
    @PostMapping("changeEnable")
    public Result changeEnable(@Validated CategoryChgEnableDto dto){
        categoryService.changeEnable(dto.getCategoryId(),dto.getEnable().getValue());
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除商品类目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品类目id", paramType = "path", dataType = "long")
    })
    @PostMapping("del/{id}")
    public Result delById(@PathVariable long id) {
        return categoryService.delById(id) > 0 ? ResultUtil.success() : ResultUtil.error("存在下级分类不允许删除");
    }


    @ApiOperation(value = "类目结构树")
    @PostMapping("tree")
    public Result tree(){
        CategoryQuery categoryQuery=new CategoryQuery();
        categoryQuery.setCatLevel(1);
        categoryQuery.setIsEnable(1);
        List<CategoryVo> categories = categoryService.selectList(categoryQuery);
        List<CategoryTreeVo> treeVos = Lists.newArrayList();
        if (CollUtil.isNotEmpty(categories)){
            categories.forEach(category -> {
                CategoryTreeVo treeVo = new CategoryTreeVo();
                BeanUtils.copyProperties(category,treeVo);
                wrapperTree(treeVo);
                treeVos.add(treeVo);
            });
        }
        return ResultUtil.success(treeVos);
    }

    private void wrapperTree(CategoryTreeVo treeVo){

        CategoryQuery categoryQuery=new CategoryQuery();
        categoryQuery.setParentId(treeVo.getId());
        categoryQuery.setIsEnable(1);

        List<CategoryVo> list = categoryService.selectList(categoryQuery);
        List<CategoryTreeVo> treeVos = Lists.newArrayList();
        if (CollUtil.isNotEmpty(list)){
            list.forEach(category -> {
                CategoryTreeVo vo = new CategoryTreeVo();
                BeanUtils.copyProperties(category,vo);
                wrapperTree(vo);
                treeVos.add(vo);
            });
            treeVo.setChilds(treeVos);
        }
    }
}
